package com.wuliqing.android.zte.data;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;

import com.wuliqing.android.zte.presenter.LoadContactListPresenter;

import java.io.InputStream;
import java.io.Serializable;

/**
 * Created by 10172915 on 2015/7/7.
 */
public class ContactData implements Serializable {
    public static final String CONTACT_DATA_TAG = "contact_data_tag";
    private String contact_name;
    private String contact_number;
    private long contact_id;
    private String contact_email;
    private Bitmap contact_photo_bitmap;
    private String lookup_key;

    public ContactData() {
    }

    public ContactData(String contact_name, String contact_number, long contact_id, String contact_email, Bitmap contact_photo_bitmap) {
        this.contact_name = contact_name;
        this.contact_number = contact_number;
        this.contact_id = contact_id;
        this.contact_email = contact_email;
        this.contact_photo_bitmap = contact_photo_bitmap;
    }

    public String getLookup_key() {
        return lookup_key;
    }

    public void setLookup_key(String lookup_key) {
        this.lookup_key = lookup_key;
    }

    public String getContact_name() {
        return contact_name;
    }

    public void setContact_name(String contact_name) {
        this.contact_name = contact_name;
    }

    public String getContact_number() {
        return contact_number;
    }

    public void setContact_number(String contact_number) {
        this.contact_number = contact_number;
    }

    public long getContact_id() {
        return contact_id;
    }

    public void setContact_id(long contact_id) {
        this.contact_id = contact_id;
    }

    public String getContact_email() {
        return contact_email;
    }

    public void setContact_email(String contact_email) {
        this.contact_email = contact_email;
    }

    public Bitmap getContact_photo_bitmap() {
        return contact_photo_bitmap;
    }

    public void setContact_photo_bitmap(Bitmap contact_photo_bitmap) {
        this.contact_photo_bitmap = contact_photo_bitmap;
    }

    @Override
    public String toString() {
        return "ContactData{" +
                "contact_name='" + contact_name + '\'' +
                ", contact_number='" + contact_number + '\'' +
                ", contact_id=" + contact_id +
                ", contact_email='" + contact_email + '\'' +
                ", contact_photo_bitmap=" + contact_photo_bitmap +
                '}';
    }

    public static ContactData fromCursor(Cursor cursor,Context context) {
        ContactData mContactData = new ContactData();
        if (cursor != null && cursor.getCount() != 0) {
            String name = cursor.getString(LoadContactListPresenter.NAME_COLUMN);
            long id = cursor.getLong(LoadContactListPresenter.ID_COLUMN);
            String lookupkey = cursor.getString(LoadContactListPresenter.LOOKUP_KEY_COLUMN);
            mContactData.setContact_id(id);
            mContactData.setContact_name(name);
            mContactData.setLookup_key(lookupkey);
            bindContactPhoto(mContactData, cursor, context);
        }
        return mContactData;
    }
    private static void bindContactPhoto(ContactData data, Cursor cursor, Context context) {
        int photoId = cursor.getInt(LoadContactListPresenter.PHOTO_ID_COLUMN);
        if (photoId > 0) {
            long contactID = data.getContact_id();
            Uri uri = ContentUris.withAppendedId(
                    ContactsContract.Contacts.CONTENT_URI, contactID);
            InputStream input = ContactsContract.Contacts
                    .openContactPhotoInputStream(
                            context.getContentResolver(), uri);
            data.setContact_photo_bitmap(BitmapFactory.decodeStream(input));

        } else {
            data.setContact_photo_bitmap(null);
        }
    }
}
